package com.jshop.seckill.web.controller;

import com.jshop.seckill.domain.UserAddress;
import com.jshop.seckill.http.HttpResult;
import com.jshop.seckill.web.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 保存收件人地址信息
 * 查询登录人关联的收件人地址列表
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    /**
     * 查询地址列表
     * @return
     */
    @GetMapping("/findAll")
    List<UserAddress> getAddressListAll(){
        return addressService.getAddressListAll();
    }

    /**
     * 给当前用户添加地址
     * @return
     */
    @PostMapping("/add")
    public HttpResult addAddress(@RequestBody UserAddress userAddress){
        HttpResult result=addressService.addAddress(userAddress);
        return result;
    }

    /**
     * 给当前用户更新地址
     * @return
     */
    @PostMapping("/update")
    public HttpResult updateAddress(@RequestBody UserAddress userAddress){
        HttpResult result=addressService.updateAddress(userAddress);
        return result;
    }

    /**
     * 根据用户id查询当前用户下的地址列表
     * @param userId
     * @return
     */
    @GetMapping("/findAddr")
    public HttpResult getAddrByUserId(@RequestParam Integer userId){
        List<UserAddress> list=addressService.getAddrByUserId(userId);
        HttpResult result = new HttpResult();
        result.setData(list);
        return result;
    }

    /**
     * 根据地址id查询当前地址值
     * @param id
     * @return
     */
    @GetMapping("/findOne")
    public HttpResult findAddrById(@RequestParam Integer id){
        UserAddress address=addressService.findByAid(id);
        HttpResult result = new HttpResult();
        result.setData(address);
        return result;
    }

    /**
     * 根据id删除当前地址
     * @param addressId
     * @return
     */
    @GetMapping("/delete")
    public HttpResult deleteAddressById(@RequestParam Integer addressId){

        return addressService.deleteById(addressId);
    }
}
